package com.example.onlineeducation.ui.mine

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.constant.ExamConstants
import com.example.onlineeducation.data.model.ExamRecordListDataModel
import com.example.onlineeducation.databinding.ExamRecordActicityBinding
import com.example.onlineeducation.databinding.ExamRecordItemBinding
import com.example.onlineeducation.viewmodel.ExamRecordVM
import com.yc.jetpacklib.extension.*
import com.yc.jetpacklib.recycleView.YcPagingDataAdapter
import com.yc.jetpacklib.refresh.YcRefreshBaseUtil
import com.yc.jetpacklib.refresh.YcRefreshSpecialPagingUtil

/*
* 考试记录列表
* */
@SuppressLint("SetTextI18n")
class ExamRecordActivity : BaseActivity<ExamRecordActicityBinding>(ExamRecordActicityBinding::inflate) {
    private val mViewModel: ExamRecordVM by ycViewModels()
    private lateinit var mRefreshUtil: YcRefreshBaseUtil<ExamRecordListDataModel>
    private var mProId: String? = null

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, ExamRecordActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mAdapter: YcPagingDataAdapter<ExamRecordListDataModel, ExamRecordItemBinding> by YcPagingDataAdapter.ycLazyInitApply(
        ExamRecordItemBinding::inflate, ExamRecordListDataModel.diffCallback
    ) {
        mOnUpdate = { item ->
            ivExam.ycLoadImageNet(item.examImage,errorImg = R.drawable.ic_topic)
            tvExamName.text = item.examName
            tvTotalScore.text = item.totalScore
            tvScore.text = item.score
            tvExamTime.text = item.examTime
            tvState.text = ExamConstants.showResultText(item.isPass)
            tvState.ycSetTextColorRes(ExamConstants.showResultTextColor(item.isPass))
            ivState.ycLoadImageRes(ExamConstants.showResultIcon(item.isPass))
        }
        mItemClick = {
            if (it.examLogId.ycIsNotEmpty())
                ExamResultDetailActivity.newInstanceExamRecord(this@ExamRecordActivity, it.examLogId!!, it.examId, it.examName, it.totalTime, it.endTime)
            else showToast("考试记录Id无效,请联系管理员")
        }
    }

    override fun ExamRecordActicityBinding.initView() {
        actionbarLayout.setTitle(titleName = "考试记录")
        rvList.apply {
            adapter = mAdapter
            ycInitLinearLayoutManage()
        }
        mRefreshUtil = YcRefreshSpecialPagingUtil<ExamRecordListDataModel>(this@ExamRecordActivity)
            .build(mAdapter, srlList, rvList, flRefresh) {
                mRefreshCall = {
                    getData()
                }
            }
        mRefreshUtil.startRefresh()

        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null) {
                mProId = it.proId
            } else {
                showToast("项目Id为空，请联系管理员")
            }
        }
        mViewModel.mExamRecordListDataModel.observe {
            mRefreshUtil.acSetPagingData(it)
        }
    }

    private fun getData() {
        mViewModel.getExamRecordListPager(mProId!!)
    }
}